<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    //任意数求和
    function sum() {
      let total = 0
      for (let i = 0; i < arguments.length; i++) {
        const item = Number(arguments[i]);
        if (!isNaN(item))
          total += item
      }
      return total
    }
    const res = sum(1, 2, 3, 4, 5)
    console.log(res);
    // 递归 求 1-10 3的倍数的和
    function get_sum_of_one_to_ten(num) {
      if (num === 10) {
        return 0
      }
      if (num % 3 === 0) {
        return num + get_sum_of_one_to_ten(num + 1)
      } else {
        return get_sum_of_one_to_ten(num + 1)
      }
    }
    console.log(get_sum_of_one_to_ten(1));
    // 数组常用方法
    // push
    const arr1 = [1, 2, 3]
    const r1 = arr1.push(4, 5) // return the length of array
    console.log(arr1, r1);
    // pop
    const arr2 = [1, 2, 3]
    const r2 = arr2.pop() // return the poped element
    console.log(arr2, r2);
    //unshift
    const arr3 = [1, 2, 3]
    const r3 = arr3.unshift(5, 6)
    console.log(arr3, r3);
    // shift
    const arr4 = [1, 2, 3]
    const r4 = arr4.shift()
    console.log(arr4, r4);
    // splice
    const arr5 = [1, 2, 3]
    const r5 = arr5.splice(1, 1)
    console.log(arr5, r5); // [1,3],[2]
    // splice 
    const arr6 = [1, 2, 3]
    const r6 = arr6.splice(1, 1, 3, 4) //第三个参数后面都是要添加进数组的
    console.log(arr6, r6);
    // splice
    const arr7 = [1, 2, 3]
    const r7 = arr7.splice(1, 0, 3, 4) //第三个参数后面都是要添加进数组的
    console.log(arr7, r7); // 在下标为1元素的前面添加
    // slice 不改变原数组
    const arr8 = [1, 2, 3, 4, 5]
    const r8_1 = arr8.slice(1, 3) // 左闭右开区间
    const r8_2 = arr8.slice(1)
    const r8_3 = arr8.slice()
    const r8_4 = arr8.slice(0)
    const r8_5 = arr8.slice(-2) // 取倒数两个
    console.log(arr8, r8_1, r8_2, r8_3, r8_4, r8_5);
    // concat 不改变原数组
    const arr9 = [1, 2, 3, 4, 5]
    const r9_1 = arr9.concat([6, 7])
    const r9_2 = arr9.concat(6, 7)
    const r9_3 = arr9.concat()
    console.log(arr9, r9_1, r9_2, r9_3, r9_3 === arr9);
    // join
    const arr10 = [1, 2, 3, 4, 5]
    const r10 = arr10.join('') // return string
    console.log(arr10, r10);
    // toString
    const arr11 = [1, 2, 3, 4, 5]
    const r11 = arr11.toString()  // 数组转化成字符串，逗号隔开
    console.log(arr11, r11);
    // indexOf lastIndexOf 
    // sort reverse 改变原数组
    // forEach 没有返回值 不改变原数组
    const arr12 = [1, 2, 3, 4, 5]
    const r12 = arr12.forEach(p => { return p * p })
    console.log(arr12, r12); // return undefined
    // map 回调函数的返回值组成的新数组，不改变原数组
    const arr13 = [1, 2, 3, 4, 5]
    const r13 = arr13.map(p => p * p)
    console.log(arr13, r13);

  </script>
</body>

</html>